// 文件路径：com/ruoyi/system/service/impl/BkEmailServiceImpl.java
package com.ruoyi.system.service.impl;

import com.ruoyi.system.service.IBkEmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

@Service
public class BkEmailServiceImpl implements IBkEmailService { // 实现统一接口
    private static final Logger log = LoggerFactory.getLogger(BkEmailServiceImpl.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.nickname:博客评论通知}")
    private String nickname;

    @Override
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        try {
            // 设置带昵称的发件人
            helper.setFrom(new InternetAddress(from, nickname, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("设置邮件发件人昵称失败，使用默认地址: {}", from, e);
            helper.setFrom(from);
        }

        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);  // HTML格式

        mailSender.send(message);
        log.info("评论回复通知邮件发送成功，收件人: {}", to); // 使用日志替代System.out
    }
}